﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ConsumerClient.ServiceReference1;

namespace ConsumerClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        PlanRouteServiceClient client;

        private WCFUser mainUser = null;
        private ICollection<WCFCar> Cars;


        public MainWindow()
        {
            InitializeComponent();
            client = new PlanRouteServiceClient();
        }

      

        private void Window_ContentRendered(object sender, EventArgs e)
        {
            this.IsEnabled = false;

            Login login = new Login(client);
            login.Left = this.Left + (this.Width / 2) - (login.Width / 2);
            login.Top = this.Top + (this.Height / 2) - (login.Height / 2);
            //login.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            if (login.ShowDialog() == true)
            {
                mainUser = login.LoginUser;
                Title = "Electric Car Client: " + mainUser.Name;
                UpdateUserDetails();
                Cars = mainUser.Cars;

                this.IsEnabled = true;

                GenerateDatagrid();
                RefreshDatagrid();
            }
            else
            {
                this.Close();
            }
        }

        private void btn_newReservation_Click(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;

            NewReservation newRes = new NewReservation(mainUser,client);
            newRes.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            newRes.ShowDialog();
            RefreshDatagrid();
            this.IsEnabled = true;
        }

        private void btn_logout_Click(object sender, RoutedEventArgs e)
        {
            //Application ne = new Application();
            ////Application.Restart();
            //Application.Current.Shutdown();

            MainWindow newSession = new MainWindow();
            newSession.Show();
            this.Close();
            
        }

        private void GenerateDatagrid()
        {
            dgr_activeReservations.AutoGenerateColumns = false;
            dgr_activeReservations.IsReadOnly = true;
            dgr_activeReservations.CanUserResizeColumns = false;
            dgr_activeReservations.CanUserResizeRows = false;
            dgr_activeReservations.CanUserReorderColumns = false;
            dgr_activeReservations.CanUserSortColumns = false;
            

            DataGridTextColumn date = new DataGridTextColumn();
            date.Header = "Dato";
            date.Binding = new Binding("Date");
            date.Width = 80;
            dgr_activeReservations.Columns.Add(date);

            DataGridTextColumn from = new DataGridTextColumn();
            from.Header = "Fra";
            from.Binding = new Binding("From");
            from.Width = 100;
            dgr_activeReservations.Columns.Add(from);

            DataGridTextColumn destination = new DataGridTextColumn();
            destination.Header = "Til";
            destination.Binding = new Binding("Destination");
            destination.Width = 100;
            dgr_activeReservations.Columns.Add(destination);

            DataGridTextColumn stops = new DataGridTextColumn();
            stops.Header = "Antal stop";
            stops.Binding = new Binding("NoOfStations");
            stops.Width = 80;
            dgr_activeReservations.Columns.Add(stops);

            DataGridTextColumn car = new DataGridTextColumn();
            car.Header = "Bil";
            car.Binding = new Binding("Car");
            car.Width = 115;
            dgr_activeReservations.Columns.Add(car);
        }

        private async void RefreshDatagrid()
        {
            Task<WCFReservation[]> getList = client.GetActiveReservationsByUserDateAsync(mainUser, DateTime.Today);

            ICollection<WCFReservation> list = await getList;
            
            List<WCFReservation> sortedList = list.OrderBy(i => i.Date).ToList();

            ICollection<DataGridItem> items = new List<DataGridItem>();
            
            foreach (WCFReservation r in sortedList)
            {
                items.Add(new DataGridItem(r));
            }
            
            //List<DataGridItem> sortedList = items.OrderBy(i => i.Date).ToList();
            
            dgr_activeReservations.ItemsSource = items;

            

            

            
            //System.ComponentModel.ListSortDirection
            //dgr_activeReservations.ItemsSource = client.GetActiveReservationsByUserDate(mainUser, DateTime.Today);
            //dgr_activeReservations.ColumnFromDisplayIndex(0).Visibility = System.Windows.Visibility.Hidden;
        }

        private void btn_profile_Click(object sender, RoutedEventArgs e)
        {
            UpdateUser uDialog = new UpdateUser(mainUser,client);
            uDialog.Top = this.Top + 16;
            uDialog.Left = this.Left + 8;
            if (uDialog.ShowDialog() == true)
            {
                mainUser = uDialog.User;
                UpdateUserDetails();
            }
        }

        private void UpdateUserDetails()
        {
            txt_name.Text = mainUser.Name;
            txt_address.Text = mainUser.AddressLine1;
            if (mainUser.AddressLine2 != null && mainUser.AddressLine2 != "")
            {
                txt_address.Text += "\r\n";
                txt_address.Text += mainUser.AddressLine2;
            }
            txt_address.Text += "\r\n";
            txt_address.Text += mainUser.ZipCodeCity.ZipCode;
            txt_address.Text += " ";
            txt_address.Text += mainUser.ZipCodeCity.City;
            txt_phone.Text = mainUser.Phone;
            lst_cars.Items.Clear();
            foreach (WCFCar car in mainUser.Cars)
            {
                lst_cars.Items.Add(car.RegistrationNo);
            }
        }
    }
}
